package com.sky.controller.user;

import com.sky.entity.Dish;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController("UserDishController")
@RequestMapping("/user/dish")
@Slf4j
public class DishController {


    @Autowired
    private DishService dishService;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 根据分类获取菜品
     * @param categoryId
     * @return
     */
    @GetMapping("/list")
    public Result<List<DishVO>> list(Long categoryId){

        log.info("categoryId:{}", categoryId);

//        //1、定义缓存key
//        String key = "dish_" + categoryId;
//        //2、从缓存里面读取数据
//        List<DishVO> list = (List<DishVO>)  redisTemplate.opsForValue().get(key);
//        //3、读取数据有效，直接返回
//        if (list != null && list.size() > 0){
//            return Result.success(list);
//        }
        //4、如果数据无效，查询数据
        List<DishVO> list = dishService.listByCategoryId(categoryId);
        //5、写入缓存
//        redisTemplate.opsForValue().set(key,list);

        return Result.success(list);


    }
}
